//面向对象的操作
//创建对象
var that;
class Tab {
    //构造函数
    constructor(id) {
        that = this;
        //获取元素
        this.main = document.querySelector(id);
        this.add = this.main.querySelector('.tabadd');
        //li父级元素
        this.ul = this.main.querySelector('.fisrstnav ul');
        //section父级元素
        this.tabscon = this.main.querySelector('.tabscon');
        this.init();
    }
    //初始化操作绑定事件
    init() {
        this.updateNode();
        //绑定点击事件
        this.add.onclick = this.addTab;
        for (var i = 0; i < this.lis.length; i++) {
            this.lis[i].index = i;
            this.lis[i].onclick = this.toggleTab;
            this.remove[i].onclick = this.removeTab;
            this.spans[i].ondblclick = this.editTab;
            this.sections[i].ondblclick = this.editTab;
        }
    }
    //解决BUG获取所有的li和section  解决动态添加元素的问题
    updateNode() {
        this.lis = this.main.querySelectorAll('li');
        this.sections = this.main.querySelectorAll('section');
        //获取关闭按钮
        this.remove = this.main.querySelectorAll('.icon-guanbi');
        this.spans = this.main.querySelectorAll('.fisrstnav li span:first-child');
    }
    //对象所有的功能
    //1.切换
    toggleTab() {
        that.clearClass();
        this.className = 'liactive';
        that.sections[this.index].className = 'conactive';
    }
    //清除样式
    clearClass() {
        for (var i = 0; i < this.lis.length; i++) {
            this.lis[i].className = ''
            this.sections[i].className = ''
        }
    }
    //2.添加
    addTab() {
        //添加的新方法
        that.clearClass();
        var li = '<li class="liactive"><span>新增选项卡</span><span class="iconfont icon-guanbi"></span></li>';
        var section = '<section class="conactive">新增模块</section>';
        that.ul.insertAdjacentHTML('beforeend', li);
        that.tabscon.insertAdjacentHTML('beforeend', section);
        //每次添加后重新获取一次所需元素
        that.init();
    }
    //3.删除
    removeTab(e) {
        //阻止冒泡
        e.stopPropagation();
        var index = this.parentNode.index;
        that.lis[index].remove(); //remove可以直接删除指定元素
        that.sections[index].remove();
        // 每次删除后也重新获取一次所需元素
        that.init();
        //删除不是选定状态的Li 不做下面操作
        if (document.querySelector('.liactive')) return;
        //删除元素后让前一个元素处于选定状态
        index--;
        //当index--减没了 小妙招
        that.lis[index] && that.lis[index].click();
    }
    //4.修改
    editTab() {
        //获取原先span内容
        var str = this.innerHTML;
        //先双击禁止选定文字
        window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
        //双击变表单
        this.innerHTML = '<input type = "text" >';
        var input = this.children[0];
        input.value = str;
        input.select(); //双击后所有文字处于选中状态
        input.onblur = function () {
            this.parentNode.innerHTML = this.value;
        }
        input.onkeyup = function (e) {
            if (e.keyCode === 13) {
                this.blur();
            }
        }
    }
}
//创建实例
new Tab('#tab');